package com.heima.wemedia.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmNewsMaterial;
import com.heima.utils.threadlocal.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.mapper.WmNewsMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;

@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private WmNewsMaterialMapper wmNewsMaterialMapper;
    /**
     * 素材上传
     *
     * @param file
     * @return
     */
    @Override
    @Transactional
    public ResponseResult upload(MultipartFile file){

        WmMaterial wmMaterial = new WmMaterial();
        //1.上传图片到minio中
        try {
            String fileId = fileStorageService.uploadImgFile("", file.getOriginalFilename(), file.getInputStream());
            if(StrUtil.isBlank(fileId)){
                ResponseResult.errorResult(501,"文件上传失败");
            }

            wmMaterial.setUrl(fileId);
            wmMaterial.setUserId(WmThreadLocalUtil.getUser().getId());
            wmMaterial.setCreatedTime(LocalDateTime.now());
            wmMaterial.setType((short) 0);
            wmMaterial.setIsCollection((short) 0);
            boolean save = this.save(wmMaterial);

            //TODO

        } catch (IOException e) {
            ResponseResult.errorResult(501,"文件上传参数失效");
        }

        return ResponseResult.okResult(wmMaterial);
    }

    /**
     * 查询素材列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(WmMaterialDto dto) {
       //TODO
        //1.非空判断
        dto.checkParam();
        //2.分页查询
        IPage<WmMaterial> page=new Page<>(dto.getPage(),dto.getSize());
        IPage<WmMaterial> page1 = this.lambdaQuery().eq(dto.getIsCollection() != null, WmMaterial::getIsCollection, dto.getIsCollection())
                .page(page);
        ResponseResult result=new PageResponseResult(dto.getPage(),dto.getSize(), (int) page1.getTotal());
        result.setData(page1.getRecords());
        return result;
    }

    /**
     * 删除素材
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult del(Integer id) {
        //TODO
        if(id==null){
            ResponseResult.errorResult(501, "参数失效");
        }
        WmMaterial wmMaterial = this.lambdaQuery().eq(WmMaterial::getId, id).one();
        if(wmMaterial==null){
            ResponseResult.errorResult(1002, "数据不存在");
        }
        Integer count = wmNewsMaterialMapper.selectCount(new LambdaQueryWrapper<WmNewsMaterial>().eq(WmNewsMaterial::getMaterialId, id));
        if(count>0){
            ResponseResult.errorResult(501, "文件删除失败");
        }
        boolean b = this.removeById(id);
        if(!b){
            ResponseResult.errorResult(501, "文件删除失败");
        }
        return ResponseResult.okResult(200, "操作成功");
    }

    /**
     * 取消收藏
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult cancel(Integer id) {
        //TODO
        if(id==null){
            ResponseResult.errorResult(501,"参数失效");
        }
        //修改IsCollection为0
        WmMaterial wmMaterial = this.lambdaQuery().eq(WmMaterial::getId, id).one();
        if(wmMaterial==null){
            ResponseResult.errorResult(1002, "数据不存在");
        }
        this.lambdaUpdate().eq(WmMaterial::getId, id).set(WmMaterial::getIsCollection, 0).update();

        return ResponseResult.okResult(200,"操作成功");
    }

    /**
     * 收藏
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult collect(Integer id) {
        //TODO
        if(id==null){
            ResponseResult.errorResult(501,"参数失效");
        }
        this.lambdaUpdate().eq(WmMaterial::getId, id).set(WmMaterial::getIsCollection, 1).update();

        return ResponseResult.okResult(200,"操作成功");
    }
}
